import React from 'react'
import { connect } from 'react-redux'
import {messages} from "share/common";
import menuRoute from 'routes/menuRoute'
import {Tabs, Spin, Icon } from 'antd';
const TabPane = Tabs.TabPane;
import CityGroupDetail from 'containers/setting/city-group/city-group-info/city-group-detail';
import CityGroupAddCity from 'containers/setting/city-group/city-group-info/city-group-add-city';
import CityGroupService from "containers/setting/city-group/city-group.service";

class CityGroupInfo extends React.Component{
  constructor(props){
    super(props);
    this.state = {
      levelOID : this.props.params.levelOID,
      disabled: this.props.location.state.disabled,
      cityGroup: menuRoute.getRouteItem('city-group','key').url,
      page: 0,
      data: [],
      activeKey: messages('setting.key200'/*详细信息*/),
      loading: false,
    };
  }

  componentWillMount () {
    // this.getCityGroupCityList();
  };

  //获取城市组下的城市
  getCityGroupCityList = () => {
    const {levelOID, page} = this.state;
    this.setState({
      loading: true,
    });
    CityGroupService.getCityGroupCityList(levelOID, page).then(
      res => {
        if(res.status === 200){
          this.setState({
            data: res.data,
            loading: false,
          }, () => {
            // if(this.state.data.length){
            //   this.setState({
            //     activeKey: messages('city.level.info.detail')/*详细信息*/,
            //     loading: false,
            //   })
            // }else{
            //   this.setState({
            //     activeKey: messages('city.level.add.city')/*添加城市*/,
            //     loading: false,
            //   })
            // }
          });
        }
      }
    )
  };

  //切换tab
  tabChange = (key) => {
    this.setState({
      activeKey: key
    })
  };

  //接收子组件的消息
  getChildMessage=(message)=>{
    if(message === '添加成功'){
      this.setState({
        activeKey: messages('setting.key200'/*详细信息*/)
      })
    }
  };

  render () {
    const {levelOID, cityGroup, loading, activeKey, disabled} = this.state;
    return(
      <div>
        <Spin spinning={loading}>
          {
            activeKey ?
              <div>
                <Tabs activeKey={activeKey} onChange={this.tabChange}>
                  {/*详细信息*/}
                  <TabPane tab={messages('setting.key200'/*详细信息*/)} key={messages('setting.key200'/*详细信息*/)}>
                    <CityGroupDetail parentMessage={this.getChildMessage} levelOID={levelOID} activeKey={activeKey}/>
                  </TabPane>
                  <TabPane tab={messages('setting.key194'/*添加城市*/)} key={messages('setting.key194'/*添加城市*/)} disabled={disabled}>
                    <CityGroupAddCity parentMessage={this.getChildMessage} levelOID={levelOID} activeKey={activeKey}/>
                  </TabPane>
                </Tabs>
                      <br/>
                <div style={{marginBottom: 20}}>
                  <a onClick={() => {this.context.router.push(cityGroup)}}><Icon type="rollback" />{messages('common.back')/*返回*/}</a>
                </div>
              </div>
              : null
          }
        </Spin>
      </div>
    )

}


}
CityGroupInfo.contextTypes = {
  router: React.PropTypes.object,
};
function mapStateToProps(state) {
  return {
    tenantMode: state.main.tenantMode,
    language: state.main.language,
    company: state.login.company,
  }
}

export default connect(mapStateToProps)(CityGroupInfo);
